﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class ListData

    Private Sub ListData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dgData.DataSource = 数据
        dgData.Tag = 数据.Copy

        tsb数据行列转换_Click(Nothing, Nothing)

    End Sub

    Private Sub tsb数据行列转换_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb数据行列转换.Click

        If tsb数据行列转换.Text = "行>>列(&T)" Then

            Dim dt As DataTable = CType(dgData.Tag, DataTable)
            If dt Is Nothing OrElse dt.Rows.Count = 0 Then
                Exit Sub
            End If

            tsb数据行列转换.Text = "列>>行(&T)"

            Dim newTable As New DataTable
            For index As Integer = 0 To dt.Rows.Count - 1
                newTable.Columns.Add("第" & CStr(index + 1) & "行")
            Next

            For jdex As Integer = 0 To dt.Columns.Count - 1
                Dim dr As DataRow = newTable.NewRow
                newTable.Rows.Add(dr)
            Next

            For jdex As Integer = 0 To dt.Rows.Count - 1
                For kdex As Integer = 0 To dt.Columns.Count - 1
                    newTable.Rows(kdex)(jdex) = dt.Rows(jdex)(kdex)
                Next
            Next

            For index As Integer = 0 To newTable.Columns.Count - 1
                Select Case Val(newTable.Rows(1)(index))
                    Case 1
                        newTable.Columns(index).ColumnName &= "(添加)"
                    Case 2
                        newTable.Columns(index).ColumnName &= "(修改前)"
                    Case 3
                        newTable.Columns(index).ColumnName &= "(修改后)"
                    Case 4
                        newTable.Columns(index).ColumnName &= "(删除)"
                End Select
            Next

            dgData.DataSource = newTable
            For kex As Integer = 0 To dgData.RowCount - 1
                dgData.Rows(kex).HeaderCell.Value = dt.Columns(kex).ColumnName
            Next

            For index As Integer = 0 To dgData.ColumnCount - 1
                dgData.Columns(index).SortMode = DataGridViewColumnSortMode.NotSortable
            Next

            Dim lst主键索引 As New List(Of String)
            For index As Integer = 0 To 主键.Rows.Count - 1
                For jdex As Integer = 0 To dgData.RowCount - 1
                    If dgData.Rows(jdex).HeaderCell.Value = 主键.Rows(index)("name") Then
                        dgData.Rows(jdex).HeaderCell.Value = dgData.Rows(jdex).HeaderCell.Value & "(" & "K)"
                        lst主键索引.Add(dgData.Rows(jdex).HeaderCell.Value)
                    End If
                Next
            Next

            If 隐藏行头 = True Then
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(Me.BindingContext(dgData.DataSource), CurrencyManager)
                myCurrencyManager.SuspendBinding()
                For kex As Integer = 0 To 3
                    dgData.Rows(kex).Visible = False
                Next
                myCurrencyManager.ResumeBinding()
            End If
        Else

            tsb数据行列转换.Text = "行>>列(&T)"
            dgData.DataSource = CType(dgData.Tag, DataTable)

            If 隐藏行头 = True Then
                For index As Integer = 0 To 3
                    dgData.Columns(index).Visible = False
                Next
            End If

            For index As Integer = 0 To dgData.RowCount - 1
                Select Case CInt(Val(dgData.Rows(index).Cells(前缀 & "_Type").Value))
                    Case 1
                        dgData.Rows(index).HeaderCell.Value = "添加"
                    Case 2
                        dgData.Rows(index).HeaderCell.Value = "修改前"
                    Case 3
                        dgData.Rows(index).HeaderCell.Value = "修改后"
                    Case 4
                        dgData.Rows(index).HeaderCell.Value = "删除"
                End Select
            Next

            Dim lst主键索引 As New List(Of String)
            For index As Integer = 0 To 主键.Rows.Count - 1
                For jdex As Integer = 0 To dgData.ColumnCount - 1
                    If dgData.Columns(jdex).Name = 主键.Rows(index)("name") Then
                        dgData.Columns(jdex).HeaderText = dgData.Columns(jdex).HeaderText & "(" & "K)"
                        dgData.Columns(jdex).DisplayIndex = lst主键索引.Count
                        dgData.Columns(jdex).Frozen = True
                        lst主键索引.Add(dgData.Columns(jdex).Name)
                    End If
                Next
            Next
        End If

        每两行对比_Click(Nothing, Nothing)
    End Sub

    Private Sub tbs对比数据_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbs对比数据.Click

        If dgData.RowCount <= 1 Then
            Return
        End If

        If tsb数据行列转换.Text = "行>>列(&T)" Then
            For index As Integer = 1 To dgData.Rows.Count - 1
                For jdex As Integer = 0 To dgData.ColumnCount - 1
                    If DBNull.Value IsNot dgData.Rows(index).Cells(jdex).Value AndAlso DBNull.Value IsNot dgData.Rows(0).Cells(jdex).Value Then
                        If Not (dgData.Rows(index).Cells(jdex).Value.Equals(dgData.Rows(0).Cells(jdex).Value)) Then
                            dgData.Rows(index).Cells(jdex).Style.BackColor = Color.Red
                        Else
                            dgData.Rows(index).Cells(jdex).Style.BackColor = Color.White
                        End If
                    End If
                Next
            Next
        Else
            For index As Integer = 1 To dgData.ColumnCount - 1
                For jdex As Integer = 0 To dgData.RowCount - 1
                    If DBNull.Value IsNot dgData.Rows(jdex).Cells(index).Value AndAlso DBNull.Value IsNot dgData.Rows(jdex).Cells(0).Value Then
                        If Not (dgData.Rows(jdex).Cells(index).Value.Equals(dgData.Rows(jdex).Cells(0).Value)) Then
                            dgData.Rows(jdex).Cells(index).Style.BackColor = Color.Red
                        Else
                            dgData.Rows(jdex).Cells(index).Style.BackColor = Color.White
                        End If
                    End If
                Next
            Next
        End If

    End Sub

    Private Sub 每两行对比_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbs每两行对比.Click

        If dgData.RowCount <= 1 Then
            Return
        End If

        If tsb数据行列转换.Text = "行>>列(&T)" Then
            For index As Integer = 0 To dgData.Rows.Count - 1 Step 2
                For jdex As Integer = 0 To dgData.ColumnCount - 1
                    If DBNull.Value IsNot dgData.Rows(index).Cells(jdex).Value AndAlso DBNull.Value IsNot dgData.Rows(0).Cells(jdex).Value Then
                        dgData.Rows(index).Cells(jdex).Style.BackColor = Color.White
                        If index + 1 < dgData.RowCount AndAlso Not (dgData.Rows(index).Cells(jdex).Value.Equals(dgData.Rows(index + 1).Cells(jdex).Value)) Then
                            dgData.Rows(index + 1).Cells(jdex).Style.BackColor = Color.Red
                        Else
                            If index + 1 < dgData.RowCount Then
                                dgData.Rows(index + 1).Cells(jdex).Style.BackColor = Color.White
                            End If
                        End If
                    End If
                Next
            Next
        Else
            For index As Integer = 0 To dgData.ColumnCount - 1 Step 2
                For jdex As Integer = 0 To dgData.RowCount - 1
                    If DBNull.Value IsNot dgData.Rows(jdex).Cells(index).Value AndAlso DBNull.Value IsNot dgData.Rows(jdex).Cells(index).Value Then
                        dgData.Rows(jdex).Cells(index).Style.BackColor = Color.White
                        If index + 1 < dgData.ColumnCount AndAlso Not (dgData.Rows(jdex).Cells(index).Value.Equals(dgData.Rows(jdex).Cells(index + 1).Value)) Then
                            dgData.Rows(jdex).Cells(index + 1).Style.BackColor = Color.Red
                        Else
                            If index + 1 < dgData.ColumnCount Then
                                dgData.Rows(jdex).Cells(index + 1).Style.BackColor = Color.White
                            End If
                        End If
                    End If
                Next
            Next

        End If

    End Sub

End Class